package com.project.api.ym.dao;

import com.project.api.common.Unit;
import com.project.api.ym.model.Roles;
import com.project.api.ym.model.Rules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class RolesDao {
    @Autowired
    @Qualifier("mainJdbcTemplate")
    private JdbcTemplate mainJdbcTemplate;

    public List<Rules> Rules(long rid){
        List<Rules> rules = new ArrayList<>();
        String sql = "SELECT b.* FROM "+ Roles.tableRolesRules +" a inner join "+Rules.tableName+" b on a.rules_id=b.id where a.roles_id=?";
        try {
            rules = mainJdbcTemplate.query(
                    sql, new Object[] {rid},new BeanPropertyRowMapper(Rules.class));
        } catch (EmptyResultDataAccessException e) {//捕获查询不到
            return rules;
        }
        return rules;
    }
}
